package jianzhioffer;

// 剑指 Offer 26. 树的子结构
public class IsSubStructure {

    public boolean isSubStructure(TreeNode A, TreeNode B) {

        if (A == null || B == null) {
            return false;
        }
        // 前面两个就是对A树进行前序遍历。
        return isSubStructure(A.left, B) || isSubStructure(A.right, B) || dfs(A, B);
    }

    private boolean dfs(TreeNode a, TreeNode b) {
        if (b == null) {
            return true;
        }
        if (a == null || a.val != b.val) {
            return false;
        }
        return dfs(a.left, b.left) && dfs(a.right, b.right);
    }
}
